//
//  AddFruitViewController.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/20.
//

#import "AddFruitViewController.h"
#import <Photos/Photos.h>

@interface AddFruitViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, copy) NSString *fruitTitle;
@property (nonatomic, strong) UIImage *fruitImage;

@end

@implementation AddFruitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"添加水果";
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = @"名称";
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(self.view).offset(150);
        make.height.equalTo(@40);
    }];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = @"请输入水果名称";
    textField.delegate = self;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [textField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@50);
        make.centerX.equalTo(self.view);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];
    
    UILabel *iconLabel = [[UILabel alloc] init];
    iconLabel.font = [UIFont systemFontOfSize:14];
    iconLabel.text = @"图片";
    [self.view addSubview:iconLabel];
    [iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(textField.mas_bottom).offset(20);
        make.height.equalTo(@40);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.layer.cornerRadius = 5;
    imageView.layer.masksToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"picture_add"];
    imageView.clipsToBounds = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addFruitImage)];
    [imageView addGestureRecognizer:tapGesture];
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.height.equalTo(@100);
        make.top.equalTo(iconLabel.mas_bottom).offset(10);
    }];
    self.imageView = imageView;
    
    UIButton *addButton = [[UIButton alloc] init];
    addButton.layer.cornerRadius = 25;
    addButton.layer.masksToBounds = YES;
    addButton.backgroundColor = kThemeColor;
    addButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton setTitle:@"添加水果" forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@50);
        make.height.equalTo(@50);
        make.centerX.equalTo(self.view);
        make.top.equalTo(imageView.mas_bottom).offset(100);
    }];
}

- (void)changedTextField:(UITextField *)textField {
    
    self.fruitTitle = textField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}

- (void)addFruitImage
{
    [[ZCImageUpload shareInstance] addImageUploadBlock:^(UIImage * _Nonnull image) {
        self.fruitImage = image;
        self.imageView.image = image;
    }];
}

- (void)confirmBtnClick
{
    if (!self.fruitTitle.length) {
        [kAppWindow makeToast:@"请输入水果名称" duration:2 position:CSToastPositionTop];
        return;
    }
    if (!self.fruitImage) {
        [kAppWindow makeToast:@"请上传水果图片" duration:2 position:CSToastPositionTop];
        return;
    }
    
    NSString *key = [NSString stringWithFormat:@"%ld", [LocalData allHomeFruitsCount]];
    NSString *imageStr = [NSObject UIImageToBase64Str:self.fruitImage];
    
    FruitModel *model = [[FruitModel alloc] init];
    model.vege_id = key;
    model.title = self.fruitTitle;
    model.imageType = 1;
    model.imageStr = imageStr;
    
    [LocalData saveHomeFruitInfoModel:model];
    
    [self.navigationController popViewControllerAnimated:YES];
    [kAppWindow makeToast:@"添加成功！" duration:2 position:CSToastPositionTop];
}

@end
